import java.net.*;
import java.io.*;
public class weather{
	
	private boolean getTemp=false;

	public void readDataFromInternet(){
		String place="Sihu";
		String address="http://www.cwb.gov.tw/V7/observe/24real/Data/C0K28.htm";
		System.out.println(place);
		try{
			URL url = new URL(address);
			URLConnection connection = url.openConnection();
			BufferedReader bfReader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
			String line="";
			while((line=bfReader.readLine())!=null){
				getWeather(line);
				//System.out.println(line);
			}
			bfReader.close();

		}catch (UnknownHostException e){
			e.printStackTrace();	
		}catch (IOException e){
			System.out.println("Maybe change the address!!");
		}
	}
	private void getWeather(String input) {
		String temperature="";
		String timeData="";
		//The temperature script
		String temp1Start="<td class=\"temp1\">";
		String temp1End ="</td><td class=\"temp2\">";
		//The time script
		String timeStart="<tr><th>";
		String timeEnd="</th><td class=\"temp1\">";
		if(input.indexOf(timeStart) != -1){
			if(input.indexOf(timeEnd) != -1){
				
				timeData = input.substring(input.indexOf(timeStart)+timeStart.length(), input.indexOf(timeEnd));
				System.out.print(timeData+"\t");
			}
				
		}
		if(input.indexOf(temp1Start) != -1){	
			temperature=input.substring(input.indexOf(temp1Start)+temp1Start.length(), input.indexOf(temp1End));
			System.out.println(temperature);
		}
		
		
	}
	public static void main(String[] args){
		weather w = new weather();
		w.readDataFromInternet();
	}	
		
}
